package com.cn.forum.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.cn.forum.dao.UserDao;
import com.cn.forum.model.User;
import com.cn.forum.service.UserService;

@Controller
public class UserController {
	@Resource
	private UserService userService;
	@Resource
	private UserDao userdao;
	
@RequestMapping("goLoginPage")
 public String goLoginPage() {
	 	return "login";
		}


 
 @RequestMapping(value="/doLogin" )
	public String doLogin(HttpServletResponse response,User user,HttpSession session,Model model) {
	    response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		User mySqlUser=userService.findUserByUserName(user.getUsername());
		if(mySqlUser!=null)
		{
		    session.setAttribute("usr", mySqlUser);
			return "redirect:"+"articleList";
			
		}
		else
		{
			 userdao.save(user);
		     session.setAttribute("usr", user);
		     return "redirect:"+"articleList";
			
		}	
}
 
        
/*@RequestMapping("/logout")
public void logout(HttpServletResponse response,  HttpSession session) throws Exception{
	session.removeAttribute("user");
	response.getWriter().print("1");
}
	
@RequestMapping("/getUserFromSession")
public void getUserFromSession(HttpServletResponse response,  HttpSession session ) throws Exception{
	response.setContentType("text/html");
	response.setCharacterEncoding("utf-8");
	JSONObject json=new JSONObject ();
	User mySqlUser=(User) session.getAttribute("user");
	if(mySqlUser!=null){
		
		json.put("isLogin", "1");
		json.put("user", mySqlUser);
	}
	else{
		json.put("isLogin", "0");
	}
	response.getWriter().print(json.toString());
}*/
}


